﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Update.WCF
{
    /// <summary>
    /// 版本信息帮助类
    /// </summary>
    public class VersionHelper
    {
        private WCFServiceRef.UpdateServiceClient _updateServiceClient;
        private string _ip;
        private int _port;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ip"></param>
        /// <param name="port"></param>
        public VersionHelper(string ip, int port)
        {
            this._ip = ip;
            this._port = port;
            _updateServiceClient = CreateUpdateServiceClient(ip, port);
        }

        /// <summary>
        /// 创建访问更新服务器的客户端对象
        /// </summary>
        /// <param name="hostConfig"></param>
        /// <returns></returns>
        private static WCFServiceRef.UpdateServiceClient CreateUpdateServiceClient(string ip, int port)
        {
            System.ServiceModel.Channels.Binding tcpBinding = new NetTcpBinding();
            (tcpBinding as NetTcpBinding).Security.Mode = SecurityMode.None;
            (tcpBinding as NetTcpBinding).MaxBufferSize = 1024 * 1024 * 1024;
            (tcpBinding as NetTcpBinding).MaxReceivedMessageSize = 1024 * 1024 * 1024;
            EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:{1}/{2}/", ip, port, "UpdateService"));

            WCFServiceRef.UpdateServiceClient updateServiceClient = new WCFServiceRef.UpdateServiceClient(tcpBinding, address);
            return updateServiceClient;
        }

        /// <summary>
        /// 更新版本信息
        /// </summary>
        /// <param name="product"></param>
        /// <param name="appType"></param>
        /// <param name="version"></param>
        /// <param name="isNecessary">此版本是不是必须要更新的</param>
        /// <returns></returns>
        public bool UpdateVersion(string product,
            string appType,
            string version,
            bool isNecessary)
        {
            try
            {
                WCFServiceRef.UpdateVersionResult result = _updateServiceClient.UpdateVersion(product, appType, version, isNecessary);
                if (result.IsSuccess)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 获取最新版本信息
        /// </summary>
        /// <param name="product"></param>
        /// <param name="appType"></param>
        /// <param name="localVersion"></param>
        /// <returns></returns>
        public CheckResult CheckVersion(string product, string appType, string localVersion)
        {
            CheckResult checkResult = new CheckResult();
            try
            {
                WCFServiceRef.CheckVersionResult result = _updateServiceClient.CheckVersion(product, appType, localVersion);

                checkResult.HasFullPatch = result.HasFullPatch;
                checkResult.IsNeedUpdate = result.IsNeedUpdate;
                checkResult.HasIncrementalPatch = result.HasIncrementalPatch;
                checkResult.LatestVersion = result.LatestVersion;
                checkResult.IsNecessary = result.IsNecessary;
            }
            catch (Exception ex)
            {
                checkResult.HasFullPatch = false;
                checkResult.IsNeedUpdate = false;
                checkResult.HasIncrementalPatch = false;
                checkResult.LatestVersion = string.Empty;
                checkResult.IsNecessary = false;
            }
            return checkResult;
        }
    }
}
